(function(angular) {
    'use strict';

    angular
        .module('xianwan')
        .factory('SystemSetting', ['$rootScope', 'LocalStorage', 'SettingConstant', '$window', '$timeout', 'Constant',  
            function($rootScope, LocalStorage, SettingConstant, $window, $timeout, Constant) {
                let currentUser = null;
                currentUser = getCurrentUser();
                let path = require('path');

                let defaultSystemSettings = {
                    'mac-os': {
                        'NOTICE': true,
                        'SOUND': true,
                        'ENTER_SHORTCUT': true, 
                        'TOGGLE_PANEL': 'Ctrl+Alt+S',  
                        'OPEN_SEARCH': 'Ctrl+Alt+F', 
                        'SCREEN_SHOT': 'Ctrl+Alt+D', 
                        'GET_MESSAGE': 'Ctrl+Alt+M', 
                        'SCREEN_SHOT_SHOW_WINDOW': true, 
                        'DOWNLOAD_PATH': getDefaultDownloadPath()
                    }, 
                    'windows-os': {
                        'NOTICE': true,
                        'SOUND': true,
                        'ENTER_SHORTCUT': true, 
                        'TOGGLE_PANEL': 'Alt+Shift+O',  
                        'OPEN_SEARCH': 'Ctrl+Shift+F', 
                        'SCREEN_SHOT': 'Ctrl+Shift+Q', 
                        'GET_MESSAGE': 'Alt+Shift+M', 
                        'SCREEN_SHOT_SHOW_WINDOW': true, 
                        'DOWNLOAD_PATH': getDefaultDownloadPath()
                    }
                };

                function getDefaultDownloadPath () {
                    let downloadPathBase = process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE;
                    let downloadPath;
                    let fs = require('fs');
                    let isWindows = process.platform !== "darwin";
                    let prefix = process.env.SystemDrive;
                    if (isWindows && downloadPathBase.indexOf(prefix) !== 0) {
                        downloadPathBase = path.join(prefix, downloadPathBase);
                    }
                    downloadPath = path.resolve(downloadPathBase, 'Downloads');
                    try {
                        if (fs.statSync(downloadPath).isDirectory()) {
                            downloadPath = path.join(downloadPath, path.sep);
                            return downloadPath;
                        }
                    } catch (e) {
                        console.log(e);
                        downloadPath = path.resolve(downloadPathBase, 'Documents');
                        downloadPath = path.join(downloadPath, path.sep);
                        return downloadPath;
                    }
                }

                function getCurrentUser() {
                    return LocalStorage.get(Constant.CURRENT_USER);
                }

                function getDefaultSystemSettings() {
                    if (process.platform == "darwin") {
                        return defaultSystemSettings['mac-os'];
                    } else if (process.platform.indexOf('win') >= 0) {
                        return defaultSystemSettings['windows-os'];
                    }
                }

                function getSystemSettings() {
                    let settings = angular.copy(getDefaultSystemSettings());
                    let tmp = null;
                    currentUser = getCurrentUser();
                    if (angular.equals(currentUser, null)) {
                        return settings;
                    }

                    let currentUserId = currentUser.user_id;
                    
                    // 当前用户的快捷键设置
                    let shortcutKeysSettings = {};

                    if (LocalStorage.get(SettingConstant.SHORTCUT_KEYS) !== null 
                        && LocalStorage.get(SettingConstant.SHORTCUT_KEYS)[currentUserId]) {
                        shortcutKeysSettings = LocalStorage.get(SettingConstant.SHORTCUT_KEYS)[currentUserId];
                    }

                    if (shortcutKeysSettings[SettingConstant.NOTICE] === false) {
                        settings[SettingConstant.NOTICE] = false;
                    }
                    if (shortcutKeysSettings[SettingConstant.SOUND] == false) {
                        settings[SettingConstant.SOUND] = false;
                    }
                    if (shortcutKeysSettings[SettingConstant.ENTER_SHORTCUT] == false) {
                        settings[SettingConstant.ENTER_SHORTCUT] = false;
                    }
                    tmp = shortcutKeysSettings[SettingConstant.TOGGLE_PANEL];
                    if (angular.isDefined(tmp) && !angular.equals(null, tmp)) {
                        settings[SettingConstant.TOGGLE_PANEL] = tmp;
                    }
                    tmp = shortcutKeysSettings[SettingConstant.OPEN_SEARCH];
                    if (angular.isDefined(tmp) && !angular.equals(null, tmp)) {
                        settings[SettingConstant.OPEN_SEARCH] = tmp;
                    }
                    tmp = shortcutKeysSettings[SettingConstant.SCREEN_SHOT];
                    if (angular.isDefined(tmp) && !angular.equals(null, tmp)) {
                        settings[SettingConstant.SCREEN_SHOT] = tmp;
                    }
                    tmp = shortcutKeysSettings[SettingConstant.GET_MESSAGE];
                    if (angular.isDefined(tmp) && !angular.equals(null, tmp)) {
                        settings[SettingConstant.GET_MESSAGE] = tmp;
                    }

                    if (shortcutKeysSettings[SettingConstant.SCREEN_SHOT_SHOW_WINDOW] === false) {
                        settings[SettingConstant.SCREEN_SHOT_SHOW_WINDOW] = false;
                    }

                    if (shortcutKeysSettings[SettingConstant.DOWNLOAD_PATH]) {
                        settings[SettingConstant.DOWNLOAD_PATH] = shortcutKeysSettings[SettingConstant.DOWNLOAD_PATH];
                    }

                    return settings;
                }

                function getSystemSettingByType(settingType) {
                    let allShortcutKeysSettings = LocalStorage.get(SettingConstant.SHORTCUT_KEYS) ? LocalStorage.get(SettingConstant.SHORTCUT_KEYS) : {};
                    currentUser = getCurrentUser();

                    if (angular.equals(currentUser, null)) {
                        return '';
                    }

                    let currentUserId = currentUser.user_id;

                    if (!allShortcutKeysSettings[currentUserId]) {
                        return getDefaultSystemSettings();
                    }

                    return allShortcutKeysSettings[currentUserId][settingType];
                }

                function changeStorageShortcut(shortcutKey, keys) {
                    let allShortcutKeysSettings = LocalStorage.get(SettingConstant.SHORTCUT_KEYS) ? LocalStorage.get(SettingConstant.SHORTCUT_KEYS) : {};
                    let shortcutKeysSettings = getSystemSettings();

                    currentUser = getCurrentUser();

                    if (angular.equals(currentUser, null)) {
                        return false;
                    }

                    let currentUserId = currentUser.user_id;

                    $rootScope.$broadcast('shortcutKeysSettingsChanged', {
                        from: 'system_setting.service.js', 
                        shortcutKey: shortcutKey, 
                        keys: keys
                    });

                    shortcutKeysSettings[shortcutKey] = keys;
                    allShortcutKeysSettings[currentUserId] = shortcutKeysSettings;

                    LocalStorage.save(SettingConstant.SHORTCUT_KEYS, allShortcutKeysSettings);
                }

                function resetCurrentUserSettings() {
                    let allShortcutKeysSettings = LocalStorage.get(SettingConstant.SHORTCUT_KEYS) ? LocalStorage.get(SettingConstant.SHORTCUT_KEYS) : {};

                    currentUser = getCurrentUser();

                    if (angular.equals(currentUser, null)) {
                        return false;
                    }

                    let currentUserId = currentUser.user_id;
                    let defaultSystemSettings = getDefaultSystemSettings();

                    allShortcutKeysSettings[currentUserId] = defaultSystemSettings;

                    LocalStorage.save(SettingConstant.SHORTCUT_KEYS, allShortcutKeysSettings);

                    // NW.changeShortcut(SettingConstant.TOGGLE_PANEL, defaultSystemSettings[SettingConstant.TOGGLE_PANEL]);
                    // NW.changeShortcut(SettingConstant.OPEN_SEARCH, defaultSystemSettings[SettingConstant.OPEN_SEARCH]);
                    // NW.changeShortcut(SettingConstant.SCREEN_SHOT, defaultSystemSettings[SettingConstant.SCREEN_SHOT]);
                }

                function getDownloadPath () {
                    const DOWNLOAD_PATH = 'DOWNLOAD_PATH';
                    let downloadPath = LocalStorage.get(DOWNLOAD_PATH);

                    if (!downloadPath) {
                        downloadPath = getDefaultSystemSettings()[DOWNLOAD_PATH];
                    }

                    return downloadPath;
                }

                function setDownloadPath () {
                    const DOWNLOAD_PATH = 'DOWNLOAD_PATH';
                    let downloadPath = LocalStorage.get(DOWNLOAD_PATH);
                }

                function changeDownloadPath (path) {
                    const DOWNLOAD_PATH = 'DOWNLOAD_PATH';

                    LocalStorage.set(DOWNLOAD_PATH, path);
                }

                return {
                    getSystemSettings: getSystemSettings, 
                    changeStorageShortcut: changeStorageShortcut, 
                    resetCurrentUserSettings: resetCurrentUserSettings, 
                    getSystemSettingByType: getSystemSettingByType, 
                    getDefaultSystemSettings: getDefaultSystemSettings, 

                    getDownloadPath: getDownloadPath
                };
            }
        ]);
})(angular);